package com.tengju.bff.interfaces.app.withdraw;

import com.tengju.bff.interfaces.app.withdraw.facade.ServiceFeeFacade;
import com.tengju.bff.interfaces.app.withdraw.vo.*;
import com.tengju.bff.interfaces.covercharge.entity.dto.IdName;
import com.tengju.bff.interfaces.shared.PhpAppResponse;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.settlement.domain.model.AgreementConfirm;
import com.tengju.settlement.domain.model.AgreementConfirmRepository;
import com.tengju.settlement.domain.model.account.UserIdCode;
import com.tengju.user.domain.model.archive.letax.IdentityType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@Api(tags = "服务费接口")
@RequestMapping("/")
public class WithdrawController {

    private final AgreementConfirmRepository agreementConfirmRepository;
    private final ServiceFeeFacade serviceFeeFacade;

    @Autowired
    public WithdrawController(AgreementConfirmRepository agreementConfirmRepository,
                              ServiceFeeFacade serviceFeeFacade) {
        this.agreementConfirmRepository = agreementConfirmRepository;
        this.serviceFeeFacade = serviceFeeFacade;
    }


    @ApiOperation(value = "服务费首页入口", notes = "服务费首页入口")
    @GetMapping(value = "withdrawal/service_fee_home", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<ServiceFeeHomeVO> serviceFeeHome() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.serviceFeeHome(userIdCode);
        } catch (Exception e) {
            log.error("服务费首页异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户预结算金额", notes = "获取用户预结算金额")
    @PostMapping(value = "newService/pre_balance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<PreBalanceVO> preBalance() {
        try {

            UserServletContext servletContext = ServletContextUtil.getServletContext();

            return serviceFeeFacade.preBalance(new UserIdCode(servletContext.needIdCode().getValue()));
        } catch (Exception e) {
            log.error("获取用户预结算金额异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "获取状态信息", notes = "获取状态信息")
    @PostMapping(value = "withdrawal/getcurrentdetail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<GetCurrentDetailVO> getCurrentDetail() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.getCurrentDetail(userIdCode);
        } catch (Exception e) {
            log.error("获取状态信息异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "设置引导状态", notes = "设置引导状态")
    @GetMapping(value = "withdrawal/serviceguide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Boolean> serviceGuide() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            return PhpAppResponse.success(serviceFeeFacade
                    .setGuideStatus(new UserIdCode(servletContext.needIdCode().getValue())));
        } catch (Exception e) {
            log.error("设置引导状态异常", e);
            return PhpAppResponse.error(e.getMessage());
        }

    }


    @ApiOperation(value = "协议确认", notes = "协议确认")
    @GetMapping(value = "withdrawal/agreement_to_confirm", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<String[]> agreementToConfirm(String type) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();

            if (!Set.of("bind", "modify", "withdraw").contains(type)) {
                return PhpAppResponse.paramError();
            }

            agreementConfirmRepository.save(new AgreementConfirm(
                    type,
                    new UserIdCode(servletContext.needIdCode().getValue()),
                    LocalDateTime.now()
            ));

            return PhpAppResponse.success(new String[]{});
        } catch (Exception e) {
            log.error("协议确认异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "提现记录状态", notes = "提现记录状态")
    @GetMapping(value = "withdrawal/withdrawal_log_status", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Object> withdrawalLogStatus() {

        try {

            UserServletContext servletContext = ServletContextUtil.getServletContext();

            return serviceFeeFacade.withdrawalLogStatus(new UserIdCode(servletContext.needIdCode().getValue()));
        } catch (Exception e) {
            log.error("提现记录状态异常", e);
            return PhpAppResponse.error(e.getMessage());
        }

    }


//    @ApiOperation(value = "个人签约(新增收款账户)", notes = "个人签约(新增收款账户)")
//    @PostMapping(value = "withdrawal/personal_service_certification", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public PhpAppResponse<Boolean> personalWithdrawalCertification(@RequestBody CertificationRequest request) {
//
//        try {
//            UserServletContext servletContext = ServletContextUtil.getServletContext();
//            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
//            return serviceFeeFacade.personalWithdrawalCertification(userIdCode, request);
//        } catch (Exception e) {
//            log.error("个人签约(新增收款账户)异常", e);
//            return PhpAppResponse.error(e.getMessage());
//        }
//
//    }

//    @ApiOperation(value = "个人签约身份证是否已被其他账号试用", notes = "个人签约身份证是否已被其他账号试用")
//    @PostMapping(value = "withdrawal/individual_contract_id_check_whether_there_is_any", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public PhpAppResponse<Boolean> individualContractIdCheckWhetherThereIsAny(@RequestBody CertificationRequest request) {
//        try {
//            UserServletContext servletContext = ServletContextUtil.getServletContext();
//            return serviceFeeFacade.individualContractIdCheckWhetherThereIsAny(
//                    new UserIdCode(servletContext.needIdCode().getValue()),
//                    request
//            );
//        } catch (Exception e) {
//            log.error("个人签约身份证是否已被其他账号试用异常", e);
//            return PhpAppResponse.error(e.getMessage());
//        }
//    }


//    @ApiOperation(value = "个人身份修改收款账户信息", notes = "个人身份修改收款账户信息")
//    @PostMapping(value = "withdrawal/person_edit_bank_info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public PhpAppResponse<Boolean> editPersonBankInfo(@RequestBody WithdrawRequest request) {
//        try {
//            UserServletContext servletContext = ServletContextUtil.getServletContext();
//            return serviceFeeFacade.editPersonBankInfo(new UserIdCode(servletContext.needIdCode().getValue()), request);
//        } catch (Exception e) {
//            log.error("个人身份修改收款账户信息异常", e);
//            return PhpAppResponse.error(e.getMessage());
//        }
//    }


    @ApiOperation(value = "收款账户信息", notes = "收款账户信息")
    @GetMapping(value = "withdrawal/bank_info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<BankVO> bankInfo(String identity) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            IdentityType identityType = IdentityType.fromVal(identity);

            //todo
            return serviceFeeFacade.getBankInfo(new UserIdCode(servletContext.needIdCode().getValue()), identityType);
        } catch (Exception e) {
            log.error("收款账户信息异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "获取用户电话号码", notes = "获取用户电话号码")
    @GetMapping(value = "withdrawal/registered_phone", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<RegisterPhoneVO> registeredPhone(String identity) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            return serviceFeeFacade.registeredPhone(new UserIdCode(servletContext.needIdCode().getValue()));
        } catch (Exception e) {
            log.error("获取用户电话号码异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "注册乐税个体户校验", notes = "注册乐税个体户校验")
    @PostMapping(value = "withdrawal/add_registered_check", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Boolean> addRegisteredCheck(@RequestBody CertificationRequest request) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            return serviceFeeFacade.addRegisteredCheck(new UserIdCode(servletContext.needIdCode().getValue()), request);
        } catch (Exception e) {
            log.error("注册乐税个体户校验异常", e);
            return PhpAppResponse.error(400, e.getMessage());
        }
    }

    @ApiOperation(value = "注册乐税个体户", notes = "注册乐税个体户")
    @PostMapping(value = "withdrawal/add_registered", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Long> addRegistered(@RequestBody RegisterRequest request) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.newAddRegistered(userIdCode, request);
        } catch (Exception e) {
            log.error("注册乐税个体户校验异常", e);
            return PhpAppResponse.paramError(e.getMessage());
        }
    }


    @ApiOperation(value = "个体工商户注册信息", notes = "个体工商户注册信息")
    @GetMapping(value = "withdrawal/registered_info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<RegisteredInfoVO> registeredInfo(String identity) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.registeredInfo(userIdCode);
        } catch (Exception e) {
            log.error("个体工商户注册信息异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "乐税H5登录", notes = "乐税H5登录")
    @GetMapping(value = "withdrawal/h5_login", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<String> h5Login(String token) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            String url = serviceFeeFacade.h5Login(userIdCode);
            return PhpAppResponse.success(url);
        } catch (Exception e) {
            log.error("乐税H5登录异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查看刷脸状态", notes = "查看刷脸状态")
    @GetMapping(value = "withdrawal/search_user_status", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Integer> searchUserStatus(String token) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.searchUserStatus(userIdCode);
        } catch (Exception e) {
            log.error("查看刷脸状态异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "更新个体户状态", notes = "更新个体户状态")
    @GetMapping(value = "withdrawal/update_registered_status", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Boolean> updateRegisteredStatus() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.updateRegisteredStatus(userIdCode);
        } catch (Exception e) {
            log.error("更新个体户状态异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "更新提现记录信息", notes = "更新提现记录信息")
    @PostMapping(value = "withdrawal/update_withdrawal_log", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<Boolean> updateWithdrawalLog(@RequestBody WithdrawRequest request) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            return serviceFeeFacade.updateWithdrawalLog(new UserIdCode(servletContext.needIdCode().getValue()), request);
        } catch (Exception e) {
            log.error("更新提现记录信息异常", e);
            return PhpAppResponse.error(500, e.getMessage());
        }
    }


    @ApiOperation(value = "获取协议", notes = "获取协议")
    @GetMapping(value = "withdrawal/article_info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<String> articleInfo(@RequestParam("id") String id) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            new UserIdCode(servletContext.needIdCode().getValue());
            String content = serviceFeeFacade.getArticleInfo(id);
            return PhpAppResponse.success(content);
        } catch (Exception e) {
            log.error("获取协议异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "注册乐税个体户二维码", notes = "注册乐税个体户二维码")
    @GetMapping(value = "withdrawal/add_registered_code", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<String> addRegisteredCode() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            String url = serviceFeeFacade.addRegisteredCode(userIdCode);
            return PhpAppResponse.success(url);
        } catch (Exception e) {
            log.error("注册乐税个体户二维码异常", e);
            return PhpAppResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "申请理由", notes = "申请理由")
    @GetMapping(value = "withdrawal/apply_for_reason", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<List<IdName>> applyForReason() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.applyForReason();
        } catch (Exception e) {
            log.error("申请理由异常", e);
            return PhpAppResponse.paramError(e.getMessage());
        }
    }

    @ApiOperation(value = "申请按钮弹框", notes = "申请按钮弹框")
    @RequestMapping(value = "withdrawal/applyPopup", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PhpAppResponse<ApplyPopupVO> applyPopup() {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = new UserIdCode(servletContext.needIdCode().getValue());
            return serviceFeeFacade.applyPopup(userIdCode);
        } catch (Exception e) {
            log.error("申请按钮弹框异常", e);
            return PhpAppResponse.paramError(e.getMessage());
        }
    }

}
